package com.deyuanyun.pic.settings.service.base.impl;

import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.domain.base.CustomThreshold;
import com.deyuanyun.pic.settings.domain.base.Threshold;
import com.deyuanyun.pic.settings.mapper.base.CustomThresholdMapper;
import com.deyuanyun.pic.settings.mapper.base.ThresholdMapper;
import com.deyuanyun.pic.settings.service.base.ThresholdManagerService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xu on 2015/12/21.
 * 阀值管理
 */
@Service
public class ThresholdManagerServiceImpl implements ThresholdManagerService {
    static Logger logger=Logger.getLogger(ThresholdManagerServiceImpl.class);

    @Autowired
    private ThresholdMapper baseThresholdMapper;
    @Autowired
    private CustomThresholdMapper customThresholdMapper;

    @Override
    public List<CustomThreshold> queryThresholdList(Threshold threshold){

        Map map=new HashMap();
        map.put("marjorCode",threshold.getMajorCode());
        map.put("minorName",threshold.getShortName());
        map.put("minorCode",threshold.getMinorCode());
        return customThresholdMapper.queryThresholdList(map);

        /*ThresholdExample example=new  ThresholdExample();
        example.setOrderByClause("SORT asc");

        ThresholdExample.Criteria criteria= example.createCriteria().andDeletedEqualTo((short) 0).andParentIdIsNotNull();
        if (StringUtils.isNotBlank(threshold.getMajorCode())){
            criteria.andMajorCodeLike("%"+threshold.getMajorCode()+"%");
        }
        if (StringUtils.isNotBlank(threshold.getShortName())){
            criteria.andShortNameLike("%"+threshold.getShortName()+"%");
        }
        return baseThresholdMapper.selectByExample(example);*/
    }


    @Override
    public List<CustomThreshold> queryThresholdGroup() {
        return customThresholdMapper.queryThresholdGroup();
    }

    @Override
    public void editThreshold(CustomThreshold threshold){
        Threshold sysBaseThreshold=new Threshold();
        sysBaseThreshold.setId(threshold.getId());
        if (threshold.getState()!=null){
            sysBaseThreshold.setState(threshold.getState());
        }else {
            Asserts.assertTrue(StringUtils.isNotBlank(threshold.getMaxVal()),"最大值不能为空");
            Asserts.assertTrue(StringUtils.isNotBlank(threshold.getMinVal()),"最小值不能为空");
            sysBaseThreshold.setMaxVal(threshold.getMaxVal());
            sysBaseThreshold.setMinVal(threshold.getMinVal());
            sysBaseThreshold.setCreateRemark(threshold.getCreateRemark());
            sysBaseThreshold.setUpdateRemark(threshold.getUpdateRemark());
        }

        baseThresholdMapper.updateByPrimaryKeySelective(sysBaseThreshold);
    }


}
